package org.lmind.example.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Streams {
	
	public static int transfer(InputStream input, OutputStream output) throws IOException {
		return transfer(input, output, 4096);
	}

	public static int transfer(InputStream input, OutputStream output, int bufferSize) throws IOException {
		byte[] buf = new byte[bufferSize];
		return transfer(input, output, buf);
	}
	
	public static int transfer(InputStream input, OutputStream output, byte[] buf) throws IOException {
		int sum = 0;
		int r = 0;
		while ((r = input.read(buf)) != -1) {
			output.write(buf, 0, r);
			sum += r;
		}
		return sum;
	}

}
